<%@ page import="org.springframework.context.support.ClassPathXmlApplicationContext" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="com.shop.service.ProductService" %>
<%@ page import="com.shop.pojo.Product" %>
<%@ page import="java.util.List" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>首页</title>
  <link href="CSS/bootstrap.min.css" rel="stylesheet"/>
  <script src="JS/index.js"></script>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-default">
  <div class="container-fluid">

    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
              data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
          <a href="#">
            <img alt="logo" src="img/shopping_mall_logo.png" style="width: 145px;height: 60px;">
          </a>
      </div>
      <%--按钮--%>
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
        <li>
          <select id="category" onchange="redirectToPage()">
            <option value="hide">--商品分类--</option>
            <option value="书籍">书籍</option>
            <option value="食品">食品</option>
            <option value="日用品">日用品</option>
            <option value="电子数码">电子数码</option>
            <option value="服装">服装</option>
          </select>
        <li><a href="#">优惠活动</a></li>
        <li><a href="#">联系我们</a></li>
      </ul>
      <%--搜索栏--%>
        <form class="navbar-form navbar-left" action="${pageContext.request.contextPath}/shop/search" method="post">
          <div class="form-group">
            <input type="text" class="form-control" name="keywords" placeholder="请输入商品关键字" style="width: 450px;" required>
          </div>
          <button type="submit" id="search_but" class="btn btn-default">搜索</button>
        </form>
        <ul id="right" class="nav navbar-nav navbar-right">
          <%--购物车--%>
          <li><a href="#"><img src="images/Shopping_cart.png" alt="cart" style="height: 23px;">购物车<span>0</span></a>
          </li>
          <%--登录--%>
          <li>
            <button class="but1" onclick="openLoginForm()">登录</button>
          </li>
          <%--注册--%>
          <li>
            <button class="but1" onclick="openRegisterForm()">注册</button>
          </li>
        </ul>
        <%--登录弹窗--%>
      <div id="loginForm" class="modal">
        <form class="modal-content" action="/LoginServlet" method="post">
          <div class="login_title">
            <h4>登录</h4>
            <button type="button" onclick="closeLoginForm()">
              <img src="img/交叉.svg" alt=""/>
            </button>
          </div>
          <div class="container">
            <div>
              <label for="username">用户名</label>
              <img src="img/登录-账号.svg" alt=""/>
                <input type="text" placeholder="输入用户名" name="username" required>
              </div>
              <div>
                <label for="password">密码</label>
                <img src="img/登录-密码.svg" alt=""/>
                <input type="password" placeholder="输入密码" name="password" required>
              </div>
            </div>
            <div class="container">
              <button type="submit" class="reg_login_but"><img src="img/登录-确认.svg" alt="" style="width: 18px;"/> 登录</button>
              <span id="forget">忘记密码？</span>
            </div>
          </form>
        </div>
        <%--注册弹窗--%>
        <div id="registerForm" class="modal">
          <form class="modal-content" action="/register" method="post">
            <div class="login_title">
              <h4>注册</h4>
              <button type="button" onclick="closeRegisterForm()">
                <img src="img/交叉.svg" alt="" />
              </button>
            </div>
            <div class="reg_container">
              <div class="responfield">
                <label for="username">用户名</label>
                <img src="img/登录-账号.svg" alt=""/>
                <input type="text" placeholder="输入用户名" name="username" required>
              </div>

              <div class="responfield">
                <label for="password">密码</label>
                <img src="img/登录-密码.svg" alt=""/>
                <input type="password" placeholder="输入密码" name="password" required>
              </div>

              <div class="responfield">
                <label for="confirm_password">确认密码</label>
                <img src="img/登录-密码.svg" alt=""/>
                <input type="password" placeholder="再次输入密码" name="confirm_password" required>
              </div>
              <div class="responfield">
                <button type="submit" class="reg_login_but"><img src="img/注册-笑脸.svg" alt="" style="width: 18px;"/> 注册</button>
              </div>
            </div>
          </form>
        </div>
      </div>
  </div>
</nav>

<%--轮播图--%>
<img src="img/向左.svg" id="left_img" alt="" onclick="lastImage()">
<div>
  <img id="image1" src="img/login.png" class="image" alt="Image 1"/>
  <img id="image2" src="img/register.png" class="image" alt="Image 2"/>
  <img id="image3" src="img/shopping_mall_logo.png" class="image" alt="Image 3"/>
</div>
<img src="img/向右.svg" id="right_img" alt="" onclick="nextImage()">

<%--展示所有商品--%>
<br>
<%
  //获取容器的业务实例，查询数据库的所有商品显示到页面
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  ProductService productService = context.getBean("productServiceImpl", ProductService.class);
  List<Product> products = productService.queryAllProduct();
  request.setAttribute("products", products);
  %>
  <div id="all">
      <c:forEach var="product" items="${products}">
        <div class="card">
          <div id="card-in">
          <div class="grid-area grid-area_a grid-thumbnail">
            <div class="card-thumbnail">
              <img src="images/${product.image}" alt="">
            </div>
          </div>
          <div class="grid-area grid-area_b">
            <h3 class="card-title">${product.productName}</h3>
            <div class="card-submit">${product.description}</div>
          </div>
          <div class="grid-area grid-area_c">
            <div class="card-label">
              <span>爆款促销</span>
              <span>金币抵2%</span>
            </div>
          </div>
          <div class="grid-area grid-area_d">
            <div class="card-price">${product.price}￥</div>
            <div class="card-action">
              <button class="but_buy">立即购买</button>
            </div>
          </div>
          </div>
        </div>
      </c:forEach>
  </div>
  <%--回到顶部--%>
<div id="box" class="box" onclick="backTop()">
  <div class="box-in" onclick="backTop()"></div>
</div>
<script>
  // 调用changeImage函数，开始播放图片
  changeImage();
  //先调用了轮播图的html结构再渲染css
  addCSS();

  function redirectToPage() {
    var category = document.getElementById("category").value;

    // 根据选项值重定向到不同的页面
    if (category == "书籍") {
      window.location.href = "books.jsp";
    } else if (category == "食品") {
      window.location.href = "foods.jsp";
    } else if (category == "日用品") {
      window.location.href = "daily.jsp";
    } else if (category == "电子数码") {
      window.location.href = "electronics.jsp";
    } else if (category == "服装") {
      window.location.href = "clothing.jsp";
    }
  }

</script>
</body>
</html>
